import 'dart:convert';
import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterapp1/Base/BaseWidget/BaseWidget.dart';
import 'package:flutterapp1/view/ATFavListItem.dart';
import 'package:flutterapp1/ATBookListWidget.dart';
import 'package:flutterapp1/color/MyColor.dart';
import 'package:flutterapp1/model/ATClassify.dart';
import 'package:flutterapp1/color/MyNetTool.dart';
import 'package:flutterapp1/view/AppNavi.dart';
class Fav extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:ATFavStatefulWidget(title: "收藏",),
    );
  }
}
class ATFavStatefulWidget extends BaseStatefulWidget{
  ATFavStatefulWidget({Key key, this.title}) : super(key: key);
  final String title;
  @override
  State<StatefulWidget> createState() {
    return _FavState();
  }
}

class _FavState extends State<ATFavStatefulWidget> {
var page = 0;
var listData = <Male>[];
var loadMoreText = "";
ScrollController _controller = new ScrollController();
  @override
  void initState(){
    super.initState();
    this.loadData();
    _controller.addListener((){
      var maxScroll = _controller.position.maxScrollExtent;
      var pixel = _controller.position.pixels;
      loadMoreText = "没有更多数据";
      new TextStyle(color: const Color(0xFF999999), fontSize: 14.0);
//      if (maxScroll == pixel && listData.length < 30) {
//        setState(() {
//          loadMoreText = "正在加载中...";
//          new TextStyle(color: const Color(0xFF4483f6), fontSize: 14.0);
//        });
//        loadData();
//      } else {
//        setState(() {
//          loadMoreText = "没有更多数据";
//          new TextStyle(color: const Color(0xFF999999), fontSize: 14.0);
//        });
//      };
    });
  }

Future<void> onRefresh() async {
  await Future.delayed(Duration(seconds: 2), (){
    page = 0;
    listData.clear();
    loadData();
  });
}
Future<void> loadData() async {
  Map<String ,dynamic> map = await MyNetTool.method(HttpMethod.Get, AppBaseUrl1, "/cats/lv2/statistics", null, null);
  Classify user = Classify.fromJson(map);
  this.listData.addAll(user.male);
  setState(() {
  });
}
Widget _buildProgressMoreIndicator() {
  return new Padding(
    padding: const EdgeInsets.all(15.0),
    child: new Center(
      child: new Text(loadMoreText, style: TextStyle(color: const Color(0xFF999999), fontSize: 14.0)),
    ),
  );
}
Widget buildListItem(BuildContext context, int index){
  Male male = this.listData[index];
  var count = male.bookCount;
  var url = male.bookCover.first;
  var subTitle = "阅读量 : $count";// Text("阅读量 : $count");
  return ATFavListItem(title: male.name,subTitle: subTitle,url: MyUrl.getNewUrl(url),onTap: (){
    Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => new ATBookListStatefulWidget(title: male.name,major: "male",)));
  },);
}
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppNavi(
        title:widget.title,
        backgroundColor: MyColor.AppColor,
        textColor: MyColor.AppFFFFFF,
        showBack: false,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              width: AppWidth,
              height: (AppHeigh - AppStatusBar - AppNaviBar - AppToolBar),
              child: new RefreshIndicator(child:CustomScrollView(
                slivers: <Widget>[
                  SliverToBoxAdapter(
                    child: Container(
                      height: 20,
                      color: MyColor.AppRed,
                      child: Text("header title 1",textAlign: TextAlign.center,),
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(buildListItem,childCount:this.listData.length),
                  ),
                  SliverToBoxAdapter(
                    child: Container(
                      height: 20,
                      color: MyColor.Appf8f8f8,
                      child: Text("header title 2",textAlign: TextAlign.center,),
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(buildListItem,childCount:this.listData.length),
                  ),
                  SliverToBoxAdapter(
                    child: Container(
                      height: 20,
                      color: MyColor.Appf8f8f8,
                      child: Text("header title 3",textAlign: TextAlign.center,),
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(buildListItem,childCount:this.listData.length),
                  ),
                ],
              ),onRefresh: onRefresh,)
            ),
          ],
        ),
      ),
    );
  }
}